﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class ReportUserStatisticsControl : System.Web.UI.UserControl
    {
        public List<MemberStatistics> memberStats = new List<MemberStatistics>();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        

        public override string ToString()
        {
            using (new SessionScope()) {
                Member[] members = Member.FindAll();
                TaskType[] TaskTypes = TaskType.FindAll();
                TaskComplexity[] TaskComplexities = TaskComplexity.FindAll();
                foreach (Member member in members) {
                    int assigned = 0;
                    int created = 0;
                    int complete = 0;
                    int incomplete = 0;
                    decimal hoursEstimated = 0m;
                    decimal hoursActual = 0m;
                    List<TaskAttributeCount> types = new List<TaskAttributeCount>();
                    List<TaskAttributeCount> complexities = new List<TaskAttributeCount>();

                    Task[] TasksAssignedTo = Task.FindAllByProperty("AssignedMemberID", member.ID);
                    Task[] TaskCreatedBy = Task.FindAllByProperty("CreateMemberID", member.ID);
                    foreach (TaskType type in TaskTypes) {
                        types.Add(new TaskAttributeCount(type.Label, 0));
                    }
                    foreach (TaskComplexity complex in TaskComplexities) {
                        complexities.Add(new TaskAttributeCount(complex.Label, 0));
                    }
                    created = TaskCreatedBy.Length;
                    assigned = TasksAssignedTo.Length;

                    foreach (Task t in TasksAssignedTo)
                    {
                        foreach (TaskAttributeCount ta in types) {
                            if (t.TaskType.Label.Equals(ta.Label)) {
                                ta.Count++;
                            }
                        }
                        foreach (TaskAttributeCount ta in complexities) {
                            if (TaskComplexity.Find(t.ComplexityEstimatedID).Label.Equals(ta.Label)) {
                                ta.Count++;
                            }
                        }
                        if (t.ClosedDateTime.Equals(new DateTime(1900,1,1))) {
                            incomplete++;
                        } else {
                            hoursActual += t.HoursActual;
                            hoursEstimated += t.HoursEstimated;
                            complete++;
                        }
                    }

                    memberStats.Add(new MemberStatistics(member.FirstName +" "+ member.LastName, created, assigned, complete, incomplete, hoursEstimated, hoursActual, types, complexities));

                }
            }
            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }      
    }
    public class TaskAttributeCount
    {
        public string Label;
        public int Count;

        public TaskAttributeCount(string label, int count)
        {
            Label = label;
            Count = count;
        }
    }
    public class MemberStatistics
    {
        public string MemberName;
        public int TasksAssignedBy;
        public int TasksAssignedTo;
        public int TasksCompleted;
        public int TasksIncomplete;
        public decimal TotalHoursEstimated;
        public decimal TotalHoursActual;
        public List<TaskAttributeCount> TaskTypeStats;
        public List<TaskAttributeCount> TaskComplexityStats;

        public MemberStatistics(string memberName, int assignedBy, int assignedTo, int complete, int incomplete, decimal estimated, decimal actual, List<TaskAttributeCount> typeStats, List<TaskAttributeCount> complexityStats)
        {
            MemberName = memberName;
            TasksAssignedBy = assignedBy;
            TasksAssignedTo = assignedTo;
            TasksCompleted = complete;
            TasksIncomplete = incomplete;
            TotalHoursEstimated = estimated;
            TotalHoursActual = actual;
            TaskTypeStats = typeStats;
            TaskComplexityStats = complexityStats;
        }
    }
}